<?php
namespace app\course\validate\coupon\admin;

use think\Validate;

/**
 * 打折卡数据验证
 */
class DiscountCard extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'mhm_id'   => 'require',
        'issue_no' => 'require',
        'discount' => 'require|between:0.1,9.9',
        'count'    => 'requireIf:online,1|checkCount',

    ];

    protected $field = [
        'mhm_id'   => '机构',
        'issue_no' => '期号',
        'discount' => '折扣',
        'count'    => '数量',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'issue_no.require' => '请输入期号',
        'mhm_id.require'   => '请选择机构',
        'discount'         => '折扣必须为0.1~9.9折',
        'count'            => '优惠券数量必须大于0',

    ];

    protected function checkCount($value, $rule, $data)
    {
        if ($data['online'] == 1 && $value < 1) {
            return '优惠券数量必须大于0';
        }

        return true;
    }
}
